﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Web.UI.HtmlControls;

namespace CoolCode.Web.Ajax.WebControls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:WebCalendar runat=server></{0}:WebCalendar>")]
    public class WebCalendar : TextBox
    {
        private const string defaultFormat = "yyyy-MM-dd";
        private const string defaultMask = "9999-99-99";
        private int width = 100;
        private CalendarExtender calExt;
        //private MaskedEditExtender maskExt;
        private bool refreshDate = false;

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            calExt = new CalendarExtender();
            calExt.ID = this.ID + "_CalendarExtender";
            calExt.Format = this.Format ?? defaultFormat;
            calExt.PopupButtonID = this.ClientID + "_CalendarDrop";
            calExt.TargetControlID = this.ID;// this.ClientID;
            calExt.CssClass = "calendar";
            if (!Page.IsPostBack)
            {
                calExt.SelectedDate = DateTime.Now.Date;
            }
            this.Controls.Add(calExt);

            /* //MaskedEditExtender在中文输入法下不能输入数字
           maskExt = new MaskedEditExtender();
           maskExt.ID = this.ID + "_MaskedEditExtender";
           maskExt.Mask = defaultMask;
           maskExt.ClearMaskOnLostFocus = false;
           maskExt.ClearTextOnInvalid = false; 
           maskExt.TargetControlID = this.ClientID; 
           this.Controls.Add(maskExt);
           */
            //maskExt.MaskType = MaskedEditType.Date;
            //maskExt.UserDateFormat = MaskedEditUserDateFormat.YearMonthDay ;

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            writeStyle();
            this.MaxLength = 10;
            if (this.Width == null || this.Width.Value < 80)
                this.Width = width;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writeScript(writer);

            if (calExt != null)
                calExt.RenderControl(writer);
            //if (maskExt != null)
            //    maskExt.RenderControl(writer);
        }

        private void writeStyle()
        {
            string cssUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "CoolCode.Web.Ajax.WebControls.WebCalendar.css");

            HtmlGenericControl cssLink = new HtmlGenericControl("LINK");
            cssLink.Attributes["rel"] = "stylesheet";
            cssLink.Attributes["type"] = "text/css";
            cssLink.Attributes["href"] = cssUrl;
            this.Page.Header.Controls.Add(cssLink);
        }

        private void writeScript(HtmlTextWriter writer)
        {
            string webResourceUrl = this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "CoolCode.Web.Ajax.WebControls.WebCalendar.png");
            //string str2 = this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "CoolCode.Web.Resources.Images.calendarDrop1.gif");
            string imageId = this.ClientID + "_CalendarDrop";
            writer.Write(string.Format("<input id='{0}' type='image' class='calendarImage' src='{1}' />", imageId, webResourceUrl));
        }

        /// <summary>
        /// Not Implement
        /// </summary>
        public string Format
        {
            get;
            set;
        }

        public DateTime? Value
        {
            get
            {
                EnsureChildControls();
                if (calExt.SelectedDate == null || !refreshDate)
                {
                    DateTime date;
                    if (DateTime.TryParse(this.Text, out date))
                    {
                        calExt.SelectedDate = date;
                        refreshDate = true;
                    }
                }
                return calExt.SelectedDate;
            }
            set
            {
                EnsureChildControls();
                calExt.SelectedDate = value;
                if (value == null)
                {
                    this.Text = string.Empty;
                }
                refreshDate = true;
            }
        }
    }
}
